import dialogHtml from 'ngtemplate!html!../../../tpls/dialog/success.dialog.html';


class RaceDetailController {
    constructor($rootScope, $scope, $cookieStore, $timeout, $state, ngDialog, dialogService, RaceService, CpService, getRaceDetail, getCourseList, iRunner, getCpList){

    	var vm = this;

    	vm.$rootScope    = $rootScope;
    	vm.$cookieStore  = $cookieStore;
    	vm.$timeout 	 = $timeout;
    	vm.$state   	 = $state;
    	vm.ngDialog 	 = ngDialog;
    	vm.dialogService = dialogService;
    	vm.getRaceDetail = getRaceDetail;
        vm.getCourseList = getCourseList;
        vm.getCpList     = getCpList;
        vm.RaceService   = RaceService
    	vm.iRunner 	     = iRunner;
        vm.CpService     = CpService;
        

        //科目list
        vm.courselist = vm.getCourseList.data;

        // 计时点列表
        vm.getCpList.code==0&&(vm.cplist = vm.getCpList.data);

        // 检查摄像头绑定状态
        vm.checkCamera = function() {

            // 获取123赛事id
            vm.CpService.getTrelated(vm.$cookieStore.get('access_token'), vm.$state.params.race_id)
            .then( data => {
                if (data.code == 0) {
                    $.get('http://www.123yd.cn/xingjiansport/V2/XiaoAi/getCameraList?raceid=' + data.data.related_id, {}, function (data) {
                        var cameras = '当前比赛用于播放视频的摄像头ID为：';
                        angular.forEach(data.result, function(data,index,array){
                            cameras += data.cameraid + ';';
                        });
                        vm.dialogService.error(cameras);
                    }, 'json');
                }
                // 调用失败, 提示用户
                (data.code!=0&&data.code!=-1001)&&vm.dialogService.error(data.message);
                // 登录信息已过期, 请重新登录
                data.code==-1001&&vm.dialogService.error(data.message, true);
            }, error => console.log('error'));
        }


        // 解绑所有设备
        vm.unbind = function() {
            // TODO
            // alert("解绑所有设备，新功能正在开发中。");
            // http://timing.com/dashboard/material/unbindall?raceid=364
            vm.RaceService.getUnbindall(vm.$cookieStore.get('access_token'), vm.$state.params.race_id)
            .then( data => {
                if (data.code == 0) {
                    vm.dialogService.success("解绑设备成功");

                }
                // 调用失败, 提示用户
                (data.code!=0&&data.code!=-1001)&&vm.dialogService.error(data.message);
                // 登录信息已过期, 请重新登录
                data.code==-1001&&vm.dialogService.error(data.message, true);
            }, error => console.log('error'));

        }
    
    }
}

RaceDetailController.$inject = ['$rootScope', '$scope', '$cookieStore', '$timeout', '$state', 'ngDialog', 'dialogService', 'RaceService', 'CpService', 'getRaceDetail', 'getCourseList', 'iRunner', 'getCpList']; 

export default RaceDetailController;